package com.spukmk3me.util;

public class NameGenerator
{
    public NameGenerator()
    {
        m_curString = null;
    }
    
    public String getNextName()
    {
        if ( m_curString == null )
            m_curString = "a";
        else
        {
            char[] chars = m_curString.toCharArray();
            int index = m_curString.length() - 1;
            char nextChar = getNextChar( chars[ index ] );
            
            chars[ index ] = nextChar;
            
            while ( nextChar == 0 )
            {
                chars[ index ] = 'a';
                
                if ( --index == -1 )
                    break;
                
                nextChar = getNextChar( chars[ index ] );
                chars[ index ] = nextChar;
            }
            
            if ( index == -1 )
                m_curString = "a" + new String( chars );
            else
                m_curString = new String( chars );
        }
        
        return m_curString;
    }
    
    private char getNextChar( char ch )
    {
        if (    ((ch >= 'a') && (ch < 'z')) ||
                (ch >= '0') && (ch < '9') )
            return (char)(ch + 1);
        else if ( ch == 'z' )
            return '0';
        else if ( ch == '9' )
            return '_';
        
        return 0;
    }
    
    private String m_curString;
}
